/*
export function someAction (context) {
}
*/
import axios from 'axios'

export async function getListPart ({ commit }, [start, count]) {
  // 框架index机制保证了获取的信息正确性
  const response = await axios.get(`/api/certs/?start=${start}&count=${count}`)
  const list = response.data
  console.log(list)
  if (!list.length) {
    // 如果返回没有东西 那就报错
    throw Error('No More')
  }
  commit('APPEND_LIST', list)
  return list
}

export async function cleanList ({ commit }) {
  commit('CLEAN_LIST')
}

export async function getInfo ({ commit }, id) {
  if (id === undefined) {
    return
  }
  const response = await axios.get(`/api/certs/${id}/`)
  const info = response.data
  commit('SET_CUR_INFO', info)
  return info
}

export async function getMyList ({ commit }) {
  const response = await axios.get('/api/certs/?all_accept=True&format=json')
  const list = response.data
  console.log('getMyList', list)
  commit('MY_LIST', list)
  return list
}

export async function getCateList ({ commit }) {
  const response = await axios.get('/api/collectcates/')
  const list = response.data
  console.log('getCateList', list)
  commit('SET_COMP_CATE', list)
  return list
}

export async function getAllCateList () {
  const response = await axios.get('/api/cates/')
  const list = response.data
  return list
}

export async function searchUserInfo ({ commit }, uid) {
  const response = await axios.get(`/api/users/${uid}/?format=json`)
  const usr = response.data
  console.log('searchUserInfo', usr)
  return usr
}

export async function getTeacherShortInfo ({ commit }, name) {
  const response = await axios.post('/api/getTeaShortInfo/', { val: name })
  const tea = response.data
  return tea
}

export async function submitCompRequest ({ commit }, form) {
  // 我们这里对表单进行格式化、提交
  const response = await axios.post('/api/certs/', form)
  // 服务器同样可以返回所提交的内容，将其放到个人相关列表中
  const info = response.data
  console.log('submitCompRequest', info)
  commit('ADD_RELATED_LIST', info)
  return info
}

export async function getUnAcceptedCerts ({ commit, dispatch }) {
  const response = await axios.get('/api/certs/?all_accept=False&format=json')
  console.log('UnAcceptedCerts', response.data)
  commit('SET_MSG_BOX', response.data)
  return response.data
}

export async function handleRequest ({ commit, dispatch }, form) {
  const response = await axios.post(`/api/certs/${form.id}/join/`, form)
  // 因为有拦截器 如果走到这里说明没问题 直接修改本地数据 或者这里直接返回数据 但是分类算法只在获取全部的时候生效 这就说明我们应该保存所有列表的
  const info = response.data
  commit('UPDATE_RELATED_LIST', info)
  return info
}

export async function getUploadPresignedURL ({ commit }, filename) {
  const response = await axios.get(`/api/presignedUrl/?name=${filename}`)
  return response.data.url
}

export async function getNewsList ({ commit }) {
  const response = await axios.get('/api/news/')
  return response.data
}

export async function getNewsDetail ({ commit }, pk) {
  const response = await axios.get(`/api/news/${pk}/`)
  return response.data
}
